package com.dream.dao.master;

/**
 * 
 * @author Evan
 * @date 2013-8-25
 * 
 */
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dream.model.master.ProductPart;
import com.dream.utils.hibernate.DetachedCriteriaBuilder;
import com.dream.utils.hibernate.DreamBaseDAO;
import com.dream.utils.hibernate.PagingVO;
import com.dream.vo.master.ProductPartVO;

@Repository
public class ProductPartDAO extends DreamBaseDAO<ProductPart> {
	
	public ProductPart getById(String id){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(ProductPart.class);
		builder.addEq("id", id);
		return this.selectTopOne(builder);
		
	}
	
	public PagingVO query(final ProductPartVO vo, final PagingVO page){
		
		DetachedCriteriaBuilder queryBuilder = initQueryBuilder(vo);
		DetachedCriteriaBuilder countBuilder = initQueryBuilder(vo);
		return this.selectPagingVO(queryBuilder, page, countBuilder);
		
	}
	
	private DetachedCriteriaBuilder initQueryBuilder(final ProductPartVO vo){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(ProductPart.class);
		builder.addEq("deleted", false).addLikeAny("name", vo.getName()).addEq("id", vo.getId());
		builder.addEq("makeType", vo.getMakeType()).addEq("level", vo.getLevel());
		builder.addEq("isBack",vo.getIsBack());
		builder.addGe("updateTime", vo.getStart()).addLe("updateTime", vo.getEnd());
		
		return builder;
		
	}
	
	/**
	 * 
	 * @param id
	 * @param name
	 * @return
	 */
	public List<ProductPart> searchProductPart(String id,String name,int topN){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(ProductPart.class);
		builder.addEq("deleted", false).addLikeAny("name", name).addEq("id", id);
		
		return this.selectTopN(builder,topN);
	}
	
}
